package com.ruoyi.project.lease.car.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.lease.customer.domain.Customer;
import com.ruoyi.project.lease.customer.service.ICustomerService;
import com.ruoyi.project.lease.order.domain.Order;
import com.ruoyi.project.lease.order.service.IOrderService;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.lease.car.domain.Car;
import com.ruoyi.project.lease.car.service.ICarService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 车辆管理Controller
 * 
 * @author admin
 * @date 2021-12-31
 */
@Controller
@RequestMapping("/lease/car")
public class CarController extends BaseController
{
    private String prefix = "lease/car";

    @Autowired
    private ICarService carService;

    @Autowired
    private ICustomerService customerService;

    @Autowired
    private IOrderService orderService;

    @RequiresPermissions("lease:car:view")
    @GetMapping()
    public String car()
    {
        return prefix + "/car";
    }

    /**
     * 查询车辆管理列表
     */
    @RequiresPermissions("lease:car:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Car car)
    {
        startPage();
        car.setDelFlag(0);
        List<Car> list = carService.selectCarList(car);
        return getDataTable(list);
    }

    /**
     * 导出车辆管理列表
     */
    @RequiresPermissions("lease:car:export")
    @Log(title = "车辆管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Car car)
    {
        car.setDelFlag(0);
        List<Car> list = carService.selectCarList(car);
        ExcelUtil<Car> util = new ExcelUtil<Car>(Car.class);
        return util.exportExcel(list, "车辆管理数据");
    }

    /**
     * 新增车辆管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存车辆管理
     */
    @RequiresPermissions("lease:car:add")
    @Log(title = "车辆管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Car car)
    {
        return toAjax(carService.insertCar(car));
    }

    /**
     * 修改车辆管理
     */
    @RequiresPermissions("lease:car:edit")
    @GetMapping("/edit/{carId}")
    public String edit(@PathVariable("carId") Long carId, ModelMap mmap)
    {
        Car car = carService.selectCarByCarIdDelFlag(carId, 0);
        mmap.put("car", car);
        return prefix + "/edit";
    }

    /**
     * 修改保存车辆管理
     */
    @RequiresPermissions("lease:car:edit")
    @Log(title = "车辆管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Car car)
    {
        return toAjax(carService.updateCar(car));
    }

    /**
     * 删除车辆管理
     */
    @RequiresPermissions("lease:car:remove")
    @Log(title = "车辆管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        if (carService.deleteCarByCarIds(ids) > 0) {
            return success();
        } else {
            return error("当前车辆状态不允许删除！");
        }
    }

    /**
     * 预览车辆信息
     */
    @RequiresPermissions("lease:car:view")
    @GetMapping( "/preview/{carId}")
    public String preview(@PathVariable("carId") long id, ModelMap mmap) {
        Car car = carService.selectCarByCarIdDelFlag(id,0);
        mmap.put("car", car);
        return prefix + "/preview";
    }

    /**
     * 点击出租车辆
     */
    @RequiresPermissions("lease:car:edit")
    @GetMapping("/rent/{carId}")
    public String rent(@PathVariable("carId") Long carId, ModelMap mmap)
    {
        Car car = carService.selectCarByCarIdDelFlag(carId, 0);
        // 状态正常的客户List
        List<Customer> customerList = customerService.selectNormalCustomer();
        mmap.put("car", car);
        mmap.put("customerList", customerList);
        return prefix + "/rent";
    }

    /**
     * 保存出租车辆
     */
    @RequiresPermissions("lease:car:edit")
    @Log(title = "车辆管理", businessType = BusinessType.RENT)
    @PostMapping("/rent")
    @ResponseBody
    public AjaxResult rentSave(@RequestParam(value = "carId") Long carId,
                                @RequestParam(value = "custNo") Long custNo,
                                @RequestParam(value = "rent_datetime") Date rent_datetime,
                                @RequestParam(value = "return_datetime") Date return_datetime)
    {
        Car car = new Car();
        car.setCarId(carId);
        car.setCustNo(custNo);
        // 车辆状态设置为待确认
        car.setCarStatus(3);
        Order order = new Order();
        order.setCustNo(custNo);
        order.setCarId(carId);
        order.setRentTime(rent_datetime);
        order.setExpectReturnTime(return_datetime);
        orderService.insertOrder(order);
        return toAjax(carService.updateCar(car));
    }

}
